﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KingApp.Common
{
    public class CombinationHelper
    {
        private int n, m;
        private int[] previous;

        public CombinationHelper(int n, int m)
        {
            this.n = n;
            this.m = m;
        }

        public static IEnumerable<IEnumerable<int>> GetCombinations(int n, int m)
        {
            List<IEnumerable<int>> combinations = new List<IEnumerable<int>>();
            var helper = new CombinationHelper(n, m);
            IEnumerable<int> combination = null;
            do
            {
                combination = helper.Next();
                if (null != combination)
                {
                    combinations.Add(combination.ToArray());
                }
            }

            while (null != combination);

            return combinations;
        }


        private IEnumerable<int> Next()
        {

            if (previous == null)
            {
                previous = new int[this.n];

                for (int i = 0; i < this.n; i++)
                {
                    previous[i] = i;
                }

                return previous.Select(p => p + 1);
            }

            int ni = n - 1, maxNi = m - 1;

            while (previous[ni] + 1 > maxNi)
            {
                //从右至左，找到有增量空间的位。

                ni--;
                maxNi--;
                if (ni < 0)
                {
                    return null;//若未找到，说明了所有的组合均已取完。
                }
            }

            previous[ni]++;

            while (++ni < n)
            {
                previous[ni] = previous[ni - 1] + 1;
            }

            return previous.Select(p => p + 1);
        }


        /// <summary>
        /// 5 选 5中 9 数字
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<int[]> GetComsFiveFiveOfNine(IEnumerable<int> codes)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            var reds = codes.ToArray();

            if (reds.Length >= 9)
            {
                dic.Add("A", reds[0]);//6
                dic.Add("B", reds[1]);//5
                dic.Add("C", reds[2]);//5
                dic.Add("D", reds[3]);//5
                dic.Add("E", reds[4]);//5
                dic.Add("F", reds[5]);//4
                dic.Add("G", reds[6]);//4 
                dic.Add("H", reds[7]);//4 
                dic.Add("I", reds[8]);//4                
            }
            else
            {
                return null;
            }

            IList<int[]> data = new List<int[]>();
            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["D"], dic["E"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["D"], dic["F"], dic["G"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["D"], dic["H"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["E"], dic["F"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["E"], dic["G"], dic["H"] });

            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["F"], dic["G"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["C"], dic["F"], dic["H"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["D"], dic["E"], dic["F"], dic["G"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["D"], dic["E"], dic["F"], dic["H"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["D"], dic["F"], dic["G"], dic["H"] });

            data.Add(new int[] { dic["A"], dic["B"], dic["D"], dic["F"], dic["G"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["B"], dic["E"], dic["G"], dic["H"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["C"], dic["D"], dic["E"], dic["F"], dic["H"] });
            data.Add(new int[] { dic["A"], dic["C"], dic["D"], dic["E"], dic["G"], dic["H"] });
            data.Add(new int[] { dic["A"], dic["C"], dic["D"], dic["F"], dic["G"], dic["I"] });

            data.Add(new int[] { dic["A"], dic["C"], dic["E"], dic["F"], dic["G"], dic["H"] });
            data.Add(new int[] { dic["A"], dic["C"], dic["E"], dic["G"], dic["H"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["D"], dic["E"], dic["F"], dic["H"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["D"], dic["E"], dic["G"], dic["H"], dic["I"] });
            data.Add(new int[] { dic["A"], dic["E"], dic["F"], dic["G"], dic["H"], dic["I"] });

            return data;

        }

    }
}
